public class SessionService extends ManagerService {

    public SessionService(CinemaSystem system) {
        super(system);
    }

    /**
     * 排片管理功能（添加/查看排片）
     */
    @Override
    public void manage() {
        super.manage();
    }

    @Override
    public void showMenu() {
        System.out.println("\n===== 排片管理 =====");
        System.out.println("1.添加场次  2.查看场次  3.返回");
        System.out.print("请选择：");
    }

    @Override
    public void add() {
        if (system.getMovies().isEmpty()) {
            System.out.println("请先添加影片！");
            return;
        }

        System.out.println("影片列表：");
        for (Movie movie : system.getMovies()) {
            System.out.println(movie.getId() + " | " + movie.getName());
        }
        System.out.print("选择影片ID：");
        String movieId = Main.sc.nextLine();
        Movie movie = getMovieById(movieId);
        if (movie == null) {
            System.out.println("影片不存在！");
            return;
        }

        System.out.println("放映厅列表：");
        for (Hall hall : system.getHalls()) {
            System.out.println(hall.getName() + "（座位：" + hall.getTotalSeats() + "）");
        }

        System.out.print("选择放映厅名称：");
        String hallName = Main.sc.nextLine();
        Hall hall = getHallByName(hallName);
        if (hall == null) {
            System.out.println("放映厅不存在！");
            return;
        }

        System.out.print("票价：");
        double price = Main.sc.nextDouble();

        Session session = new Session("S" + System.currentTimeMillis(), movie, hall, price);
        system.getSessions().add(session);
        System.out.println("场次添加成功！");
    }

    @Override
    public void showList() {
        System.out.println("\n===== 场次列表 =====");
            for (Session session : system.getSessions()) {
                System.out.println("影片：" + session.getMovie().getName()
                        + " | 厅：" + session.getHall().getName()
                        + " | 票价：" + session.getPrice());
            }
    }

    // 获取影片
    private Movie getMovieById(String movieId) {
        for (Movie movie : system.getMovies()) {
            if (movie.getId().equals(movieId)) {
                return movie;
            }
        }
        return null;
    }

    // 获取放映厅
    private Hall getHallByName(String hallName) {
        for (Hall hall : system.getHalls()) {
            if (hall.getName().equalsIgnoreCase(hallName)) {
                return hall;
            }
        }
        return null;
    }
}
